package controller

import (
	"a-common/myerr"
	"a-system/domain/pojo"
	"a-system/service"
	"fmt"
	"sync"

	"github.com/gofiber/fiber/v2"
)

var (
	logininforControllerTemp *LogininforController
	sysLogininforService     = service.NewSysLogininforService()
	logininforControllerOnce sync.Once
)

type LogininforController struct{}

func NewSysLogininforController() *LogininforController {
	logininforControllerOnce.Do(func() {
		logininforControllerTemp = &LogininforController{}
	})
	return logininforControllerTemp
}

// List 列表
func (m *LogininforController) List(c *fiber.Ctx) error {
	var args pojo.SysLogininforDto
	if err := c.QueryParser(&args); err != nil {
		return myerr.ERR_SYS_ARGS
	}
	err := args.Validator()
	if err != nil {
		return myerr.ERR_SYS_ARGS
	}

	pageVo, err := sysLogininforService.List(&args)
	if err != nil {
		return err
	}
	return Resp(c, pageVo)
}

// Export 导出
func (m *LogininforController) Export(c *fiber.Ctx) error {
	var args pojo.SysLogininforDto
	if err := c.QueryParser(&args); err != nil {
		return myerr.ERR_SYS_ARGS
	}
	f, err := sysLogininforService.Export(&args)
	if err != nil {
		return myerr.ERR_FILE_WRITE
	}
	c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, "登陆日志表.xlsx"))
	if err := f.Write(c.Response().BodyWriter()); err != nil {
		return myerr.ERR_FILE_WRITE
	}
	return nil
}

// Clear 清空
func (m *LogininforController) Clear(c *fiber.Ctx) error {
	return Ok(c)
}

// Unlock 解锁
func (m *LogininforController) Unlock(c *fiber.Ctx) error {
	userName := c.Params("userName")
	if userName == "" {
		return myerr.ERR_SYS_ARGS
	}
	// if err := global.RdCli.Del(constant.PWD_ERR_CNT_KEY + userName); err != nil {
	// 	return res.ServerFail(c, res.SYS_ERR_REDIS, err)
	// }
	return Ok(c)
}
